class AreasController < ApplicationController
#MODULE: Configuraciones

  # GET /areas
  # GET /areas.xml
  def index
  end

  # GET /areas/1
  # GET /areas/1.xml
  def show
    @area = Area.find(params[:id])    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @area }
    end
  end

  # GET /areas/new
  # GET /areas/new.xml
  def new
    @area = Area.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @area }
    end
  end

  # GET /areas/1/edit
  def edit
    @area = Area.find(params[:id])
  end

  # POST /areas
  # POST /areas.xml
  def create
    @area = Area.new(params[:area])
    
    respond_to do |format|
      if @area.save
        flash[:notice] =  'Area creada correctamente.'
        format.html { params[:oper].nil? ? redirect_to(@area) : render(:text => "Ok")}
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @area.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /areas/1
  # PUT /areas/1.xml
  def update
    @area = Area.find(params[:id])
    
    respond_to do |format|
      if @area.update_attributes(params[:area])
        flash[:notice] = 'Area actualizado correctamente.'
        format.html { redirect_to(@area) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @area.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /areas/1
  # DELETE /areas/1.xml
  def destroy
    @area = Area.find(params[:id])
    @area.destroy

    respond_to do |format|
      format.html { redirect_to(areas_url) }
      format.xml  { head :ok }
    end
  end

  def datos #IGNORE
    #Parametros
    registros, count = find_by_params(params, :class => Area.activos)
    # Construct a hash from the ActiveRecord result
    return_data = Hash.new()
    return_data[:page] = params[:page]
    return_data[:total] = (count.to_f / params[:rows].to_i).ceil
    return_data[:records] = count

    return_data[:rows] = registros.collect{|u| {:id=>u.id,
        :cell=>[ 
          u.nombre
        ]}}

    # Convert the hash to a json object
    render :text=>return_data.to_json, :layout=>false
  end
  
  def cargos #IGNORE
    area=Area.find(params[:id])
    html= <<HTML
    <select id=cargos name=cargo[id]>
HTML
    html.concat("<option value=''> Todos</option>")
    area.cargos.each do |cargo|
      html.concat("<option value=#{cargo.id}>")
      html.concat("#{cargo.nombre.html_safe}")
      html.concat("</option>")
    end
    html.concat("</select>")
    render :update do |page|
      page.replace_html :select_cargos,html
    end
  end
end
